<?php


function debug() {

	if(DEBUG) {

		$numargs = func_num_args();
		$arg_list = func_get_args();
echo "<pre>";
		for ($i = 0; $i < $numargs; $i++) {

			print_r($arg_list[$i]);
			echo "<br>";
		}
echo "</pre>";
	}
}

// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
	$filename = 'myerror.log';
	$somecontent = "<b>".date("d-m-Y G:i:s")."</b> [$errno] $errstr  --- in line $errline of file $errfile <br />\n";

	// Let's make sure the file exists and is writable first.
	if (is_writable($filename)) {

		$size = filesize($filename);
		$file_write = "a";
		if ($size > 128 * 1024 ) {
			//@unlink($filename);
			$file_write = "w";
		}

		// In our example we're opening $filename in append mode.
		// The file pointer is at the bottom of the file hence 
		// that's where $somecontent will go when we fwrite() it.
		if (!$handle = fopen($filename, $file_write)) {
			 //echo "Cannot open file ($filename)";
			 return;
		}

		// Write $somecontent to our opened file.
		if (fwrite($handle, $somecontent) === FALSE) {
			//echo "Cannot write to file ($filename)";
			//return;
		}
		fclose($handle);

	}
}

function get_domain($domain) {
	
	if(empty($domain)) return array();

	$temp = explode("://",$domain);

	if (count($temp) >= 2) {
		$mydomain = $temp[1];
	} else {
		$mydomain = $temp[0];
	}

	$temp2 = explode("/",$mydomain);

	foreach ($temp2 as $path) {

		 $path = trim( $path);

		 if (!empty($path) ) {
			 $actual = trim($path);
			 break;
		 }
	}
	if (strtolower(substr($actual,0,4)) == 'www.') {
		$with_www = $actual;
		$without_www = substr($actual,4);
	} else {
		$with_www = 'www.'.$actual;
		$without_www = $actual;
	}

	return array($with_www, $without_www);
}

function get_html_tags($text, $tagname, $closetag=true, $close_optional=false) 
{ 
        /* escape PCRE characters in tag name */ 
        $tagname = preg_quote($tagname); 
         
        $ret = array(); 

        /* regular expression to match mattributes in a tag name */ 
        //$attrib_match = "/((?i)[a-z]+) (\s*=\s* ( ((?U)(\"(.*)\")) |(\S+) ) |\s+) /sXi"; 
		$attrib_match = "/((?i)[a-z]+)(\s*=\s*(((?U)(\"(.*)\"))|(\s+))|\s+)/si";
         
        if ($closetag) { 
            if ($close_optional !== false) { 
                $close_optional = preg_quote($close_optional); 
                $regex = "/<$tagname(.*)>(.*)(<\/$tagname>|(?=<$tagname>)|(?=<\/$close_optional>))/Uis"; 
            } else { 
                $regex = "/<$tagname(.*)>(.*)<\/$tagname>/Uis"; 
            } 
        } else { 
            $regex = "/<$tagname((.+))\/?>/Uis"; 
        } 
         
        // regex now matches the tag appropriatly 
         
        preg_match_all($regex, $text, $tags, PREG_SET_ORDER); 

        $tag_count = count($tags); 
         
        for($t = 0; $t < $tag_count; $t++) { 
            $tag = array(); 
             //$tags[$t][1] = str_replace("'",'"', $tags[$t][1]);
			 
            /* get attributes */ 
            preg_match_all($attrib_match, $tags[$t][1], $attributes, PREG_SET_ORDER); 

            $attribs = array(); 

            $attrib_count = count($attributes); 
            for($a = 0; $a < $attrib_count; $a++) {     
                $name = strtolower(trim($attributes[$a][1])); 

                if (isset($attributes[$a][4])) { // name value pair found 
                    $value = trim(isset($attributes[$a][7])?$attributes[$a][7]:$attributes[$a][6]); 
                } else { 
                    $value = $name; 
                } 

                $attribs[$name] = $value;                 
            }

            if (empty($attribs['href']) && $tagname == 'a') {

				$tags[$t][1] = str_replace(array("\n","\t"),array(""," "), $tags[$t][1]);

				preg_match("/href\s*=\s*\'(.*)\'\s+/", $tags[$t][1], $second_chance);

				$attribs['href'] = @$second_chance[1];
			}

			if (empty($attribs['class']) && ($tagname == 'a' || $tagname == 'td')) {

				$tags[$t][1] = str_replace(array("\n","\t"),array(""," "), $tags[$t][1]);

				preg_match("/class\s*=([a-zA-Z0-9_]*)/", $tags[$t][1], $second_chance);

				$attribs['class'] = @$second_chance[1];
			}

            $tag['attributes'] = $attribs; 
             
            if ($closetag) { 
                $tag['text'] = $tags[$t][2]; 
            } 

            $ret[] = $tag; 
        } 

        return $ret; 
}
 /*
* This Function creates Paging
* Input: start record, total records, link to next page, page length (optional)
* OutPut: returns paging string
* Usage: Ex.  $obj->pager(0, 65, 'cases.php?', 15);
*/
#    Previous | 1 | 2 | 3 | 4 | 5 | Next
function pager($start,$total,$link,$len=10,$div='div_google') {
		$out = "";
		$en = $start+$len;
		if ($start == 0) {
			if ($total > 0) {
				$start1 = 1;
			} else {
				$start1 = 0;
			}
		} else {
			$start1 = $start+1;
		}
		if($en > $total)
			 $en = $total;
		if($total != 0)
			 $pagecount = ($total % $len)?(intval($total / $len)+1):($total / $len);
		else {
			$out.= "";
			 //$out.= "<table  cellpading=0 cellspacing=0 align=left width=100% nowrap><tr><td bgcolor='#FFFFFF' align='center' class='error'><br>No Data found. Please add data<br></td></tr></table>";
			 $pagecount = 0;
			 return $out;
		}
		$out.= "<table  cellpading=0 cellspacing=0 align=left width=100% nowrap><tr>";
		$out.= "<td width=40% class=smalltext valign=bottom height=25>Showing $start1 - $en of $total</td>";
		$out.= "<td width=54% align=right valign=bottom>";
		if($en > $len) {
			 $en1 = $start-$len;
			 $out.= "<a href=\"javascript:getsubpanel('".$link."&start=".$en1."','".$div."');\" class='smalllink'> Prev </a><span class='smalltext'> | </span>" ;
		 } else
			 $out.= "<span class='smalltext'> Prev </span><span class='smalltext'> | </span>" ;

		# Caliculating Page Values
		$numstr="";
		$curpage = intval(($start+1)/$len)+1;
		if($pagecount > 10) {
			 $istart = (intval($curpage/10) * 10)+1;
			 if($istart + 10 > $pagecount)
				 $istart = $pagecount - 9;
			 $pagecount = 10;
		} else
			 $istart=1;
		for($i = $istart; $i < $pagecount+$istart; $i++ ) {
			 $ed = ($i-1)*$len;
			 if($start != $ed) {
				  $numstr .= " <a href=\"javascript:getsubpanel('".$link."&start=".$ed."','".$div."');\" class='smalllink'> $i </a><span class='smalltext'> | </span>";
			  } else {
				  $numstr .= "<span class='smalltext'> $i </span><span class='smalltext'> | </span>";
			  }
		}
		$out.= $numstr;
		if ($en < $total ) {
			  $en2 = $start+$len;
			  $out.= "<a href=\"javascript:getsubpanel('".$link."&start=".$en2."','".$div."');\" class='smalllink'> Next </a>" ;
		} else {
			  $out.= "<span class='smalltext'> Next </span>" ;
		}
		$out.= "</td></tr></table>";
		return $out;
}

?>